<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Gateway for the events
 *
 * @category    RedCross
 * @package     Core_Gateway
 */
class Core_Gateway_Event extends RedCross_Gateway_GatewayAbstract
{

    public function __construct()
    {
        $this->_class = 'Event';
        $this->_modelPath = 'Core_Model';
    }
    
    public function fetchAllEvents($paginate = false)
    {
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' e');

        return $paginate ? new RedCross_Paginator($query) : $query->fetchArray();
    }

    public function fetchEventsThisWeek($today, $endOfWeek) {

        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' e')
            ->where('(e.from_date BETWEEN ? AND ?)', array($today->export(), $endOfWeek->export()))
            ->orWhere('(e.to_date BETWEEN ? AND ?)', array($today->export(), $endOfWeek->export()))
            ->orWhere('(e.from_date < ? AND e.to_date > ?)', array($today->export(), $endOfWeek->export()))
            ->orderBy('e.from_date ASC');

        return $query->execute();
    }

    public function fetchAllFuture($paginate = false)
    {
        $date = new RedCross_Date();

        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' e')
            ->where('e.from_date > ?', $date->export())
            ->orWhere('e.to_date > ?', $date->export());

        return $paginate ? new RedCross_Paginator($query) : $query->fetchArray();
    }

    public function fetchAllPast($paginate = false)
    {
        $date = new RedCross_Date();

        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' e')
            ->where('e.from_date < ?', $date->export())
            ->andWhere('e.to_date < ?', $date->export());

        return $paginate ? new RedCross_Paginator($query) : $query->fetchArray();
    }
    
    public function deleteEventAndChildShifts($eventId)
    {
    $query = Doctrine_Query::create()
        ->delete('Core_Model_Event e')
        ->addWhere('e.id = ?', $eventId);
        
    return $query->execute();
    }
    
    public function fetchAllWithOnlyIdAndName()
    {
    $date = new RedCross_Date();
    $query = Doctrine_Query::create()
        ->select('e.id, e.name')
        ->from('Core_Model_Event e')
        ->where('e.from_date > ?', $date->export())
            ->andWhere('e.to_date > ?', $date->export())
        ->fetchArray();
        
    return $query;
    }

    public function fetchAllByUser($userId, $paginate = false)
    {
        $query = Doctrine_Query::create()
            ->select()
            ->from($this->getModelClass() . ' e')
            ->leftJoin('e.Shifts s')
            ->leftJoin('s.Roles sr')
            ->leftJoin('sr.Member m')
            ->leftJoin('m.User u')
            ->where('u.id = ?', $userId);

        return $paginate ? new RedCross_Paginator($query) : $query->execute();
    }
}
